package club.kingyin.easycache.component.handler;

import club.kingyin.easycache.cache.ValueSerializer;
import club.kingyin.easycache.exception.SerializeException;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import org.apache.commons.text.StringEscapeUtils;

import javax.annotation.Nonnull;

public enum Serializer implements ValueSerializer<String> {

    @Deprecated
    JSON {
        @Override
        public String encode(Object data) throws SerializeException {
            String s = StringEscapeUtils.unescapeJson(com.alibaba.fastjson2.JSON.toJSONString(data));
            return s;
        }

        @Override
        public Object decode(String sources) throws SerializeException {
            try {
                if (!isJSON2(sources)) {
                    return sources;
                }
                return JSONObject.parse(sources);
            } catch (Exception e) {
                throw new SerializeException(e);
            }
        }

        @Override
        public <V> V decode(@Nonnull String sources, @Nonnull Class<V> target) throws SerializeException {
            try {
                if (!isJSON2(sources)) {
                    return target.cast(sources);
                }
                return JSONObject.parseObject(sources, target);
            } catch (Exception e) {
                throw new SerializeException(e);
            }
        }

        public boolean isJSON2(String str) {
            boolean result = false;
            try {
                Object obj = com.alibaba.fastjson2.JSON.parse(str);
                result = true;
            } catch (Exception ignored) {
            }
            return result;
        }
    },
    GSON {
        private Gson gson = new Gson();
        @Override
        public String encode(Object data) throws SerializeException {
            String s = StringEscapeUtils.unescapeJson(gson.toJson(data));
            return s;
        }

        @Override
        public Object decode(String sources) throws SerializeException {
            try {
                if (!isJSON2(sources)) {
                    return sources;
                }
                return JSONObject.parse(sources);
            } catch (Exception e) {
                throw new SerializeException(e);
            }
        }

        @Override
        public <V> V decode(@Nonnull String sources, @Nonnull Class<V> target) throws SerializeException {
            try {
                if (!isJSON2(sources)) {
                    return target.cast(sources);
                }
                return gson.fromJson(sources,target);
            } catch (Exception e) {
                throw new SerializeException(e);
            }
        }

        public boolean isJSON2(String str) {
            boolean result = false;
            try {
                Object obj = com.alibaba.fastjson2.JSON.parse(str);
                result = true;
            } catch (Exception ignored) {
            }
            return result;
        }
    }
}
